package com.abso.sunlight.explorer.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.services.IEvaluationService;

import com.abso.sunlight.api.SunlightException;
import com.abso.sunlight.explorer.CommandUtils;
import com.abso.sunlight.explorer.CongressExplorerPlugin;
import com.abso.sunlight.explorer.LegislatorSearch;
import com.abso.sunlight.explorer.dialogs.SaveLegislatorSearchDialog;
import com.abso.sunlight.explorer.editors.LegislatorListEditor;

/**
 * Saves the current search.
 */
public class SaveLegislatorSearchHandler extends AbstractHandler {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
    public Object execute(ExecutionEvent event) throws ExecutionException {
        LegislatorListEditor editor = getLegislatorListEditor(event);
        if (editor != null) {
            LegislatorSearch search = editor.getSearch();
            if (!search.isSaved()) {
                if (CongressExplorerPlugin.getSearchManager().getSearch(search.getName()) != null) {
                    SaveLegislatorSearchDialog saveDialog = new SaveLegislatorSearchDialog(HandlerUtil.getActiveShell(event), search
                            .getName());
                    if (saveDialog.open() == Window.OK) {
                        search.setName(saveDialog.getSearchName());
                    } else {
                        return null;
                    }
                }
                try {
                    CongressExplorerPlugin.getSearchManager().addSearch(search, null);
                } catch (SunlightException e) {
                    ExecutionException e2 = new ExecutionException("Unable to save", e);
                    ErrorDialog.openError(HandlerUtil.getActiveShell(event), "Error", e2.getMessage(), new Status(IStatus.ERROR,
                            CongressExplorerPlugin.PLUGIN_ID, e.getMessage(), e));
                    throw e2;
                }
                IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
                if (window != null) {
                    IEvaluationService evService = (IEvaluationService) window.getService(IEvaluationService.class);
                    evService.requestEvaluation("com.abso.sunlight.explorer.savedSearch");
                }
            }
        }
        return null;
    }

    /**
     * Returns the current legislator list editor.
     * 
     * @param event   the command's execution event.
     * @return   the current legislator list editor.
     */
    private LegislatorListEditor getLegislatorListEditor(ExecutionEvent event) {

        /* first searches for an explicit editor */
        Object o = HandlerUtil.getVariable(event, CommandUtils.LEGISLATOR_EDITOR_VAR);
        if (o instanceof LegislatorListEditor) {
            return (LegislatorListEditor) o;
        }

        /* if not found returns the active editor */
        IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
        if (activeEditor instanceof LegislatorListEditor) {
            return (LegislatorListEditor) activeEditor;
        } else {
            return null;
        }
    }

}
